package com.glory.brepda.activity.mfg;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.brepda.R;
import com.glory.brepda.adapter.MaterielListAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.base.Constant;
import com.glory.brepda.entity.EqpBody;
import com.glory.brepda.entity.EqpEntity;
import com.glory.brepda.entity.LotInfoEntity;
import com.glory.brepda.entity.MaterielListEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.LotUtils;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

public class DSWorkBeginActivity extends BaseActivity {
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.tvLotState)
    TextView tvLotState;
    @BindView(R.id.etLot)
    FreeUI_ClearEditText etLot;
    @BindView(R.id.etEqpWorkID)
    FreeUI_ClearEditText etEqpWorkID;
    @BindView(R.id.tvChip)
    TextView tvChip;
    @BindView(R.id.tvRecipeID)
    TextView tvRecipeID;
    @BindView(R.id.tvStepDesc)
    TextView tvStepDesc;
    @BindView(R.id.rvMaterielList)
    RecyclerView rvMaterielList;
    @BindView(R.id.etComment)
    FreeUI_ClearEditText etComment;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvPauseStatus)
    TextView tvPauseStatus;
    @BindView(R.id.lotScan)
    ImageView lotScan;
    @BindView(R.id.eqpScan)
    ImageView eqpScan;
    private static final int REQUEST_CODE_TEXT1 = 1001;
    private static final int REQUEST_CODE_TEXT2 = 1002;
    private MaterielListAdapter materielListAdapter;
    private List<EqpEntity> materielList;
    private List<MaterielListEntity.Materiel> partList;
    private String woid;
    private LotInfoEntity lotInfo;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_rowbegin;
    }

    @Override
    protected void initView() {
        initLayout();

        initEdit(etLot, s -> {
            //获取lot信息
            LotUtils.searchLotInfo(DSWorkBeginActivity.this, s, getSupportFragmentManager(), new LotUtils.netCallback<LotInfoEntity>() {
                @Override
                public void onSuccess(LotInfoEntity response) {
                    lotInfo = response;
                    Constant.LIMITPACKAGETYPEQTY = response.getLIMITPACKAGETYPEQTY();
                    Constant.PACKAGETYPE = response.getPACKAGETYPE();
                    Constant.WIRESPEC = response.getWIRESPEC();
                    Constant.BDCODE = response.getBD_CODE();
                    Constant.PARTNAME = response.getLOT().getPARTNAME();
                    tvPauseStatus.setText(response.getLOT().getHOLDSTATE());
                    woid = response.getLOT().getWOID();
                    tvChip.setText("");
                    tvStepDesc.setText(response.getLOT().getSTEPDESC());
                    etEqpWorkID.requestFocus();
//                    tvTitle.setText("开始作业" + "(" + response.getLOT().getSTEPDESC() + ")");
                    tvChip.setText(response.getLOT().getCUSTOMERLOTID());
                    tvLotState.setText(response.getLOT().getSTATE());
                }

                @Override
                public void onFail(Throwable t) {
                    loadError(t, "LOTQUERY");
                }

            });
        });

        initEdit(etEqpWorkID, s -> {
            partList.clear();
            materielList.clear();
            getKittingInfo(s);
        });
    }

    private void getKittingInfo(String eqpID) {
        FreeUI_EntityCallBack<BaseResponseBean<EqpEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<EqpEntity>>
                (new TypeToken<BaseResponseBean<EqpEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<EqpEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(DSWorkBeginActivity.this, true, getSupportFragmentManager())) {
                    EqpEntity body = response.body().getResponse().getBody();
                    materielListAdapter.setNewData(body.equipmentlist);
                }

            }

            @Override
            public void onError(Response<BaseResponseBean<EqpEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "Common");

            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE", "EQPBYID");
        EqpBody eqpBody = new EqpBody("",eqpID,"");
        map.add("EQUIPMENT",new JsonParser().parse(new Gson().toJson(eqpBody)).getAsJsonObject());
        NetUtils.requestPostNet("IF.BREQUERYEQP", "Common", DSWorkBeginActivity.this, map, callBack);
    }


    private void initLayout() {
        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        setMainLayout(llMain);
        initRecyclerView(rvMaterielList, this);
        etLot.requestFocus();
        materielListAdapter = new MaterielListAdapter(null);
        materielListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        materielListAdapter.isFirstOnly(true);
        rvMaterielList.setAdapter(materielListAdapter);

        materielList = new ArrayList<>();
        partList = new ArrayList<>();

    }

    @Override
    protected void initData() {

        lotScan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startQrScanner(REQUEST_CODE_TEXT1);
            }
        });
        eqpScan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startQrScanner(REQUEST_CODE_TEXT2);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK && data != null) {
            String qrResult = data.getStringExtra("SCAN_RESULT"); // 获取扫码结果
            if (qrResult != null) {
                switch (requestCode) {
                    case REQUEST_CODE_TEXT1:
                        etLot.setText(qrResult);
                        break;
                    case REQUEST_CODE_TEXT2:
                        etEqpWorkID.setText(qrResult);
                        break;
                }
            }

        }
    }



    @OnClick(R.id.tvBaseSubmit)
    public void onViewClicked() {

//        if (actionControl("Submit01")){
//            return;
//        }

        if (tvLotState.getText().toString().equals("待投产")) {
            showConfirmDialog("批次未投产，请联系计划部执行投产");
            return;
        }

        if (tvLotState.getText().toString().equals("正在作业")) {
            showConfirmDialog("批次正在作业，请直接操作出站功能");
            return;
        }

        LotUtils.lotTrackIn(getSupportFragmentManager(),lotInfo,null,woid,"DSLOTTRACKIN", this, getText(etEqpWorkID), getText(etLot), new ArrayList<String>(), "DefaultTrackIn");
    }


}
